package com.purang.web.config;

import com.purang.web.utils.HttpClientUtils;
import lombok.Data;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * {@link HttpClientUtils}配置类
 *
 * @author Chuan Qin
 * @author Chunyan Li
 * @author Zheming Liu
 * @since 1.0-RELEASE
 */
@Configuration
@EnableConfigurationProperties(HttpClientUtilsConfig.UriConfig.class)
public class HttpClientUtilsConfig {

    /**
     * 注入实例到spring容器
     */
    @Bean
    @ConfigurationProperties(prefix = "httpclient")
    public HttpClientConfig registerHttpClientConfig() {
        return new HttpClientConfig();
    }

    /**
     * HttpClient路由路径
     */
    @Data
    @ToString
    @ConfigurationProperties(prefix = "httpclient.uri")
    public class UriConfig {
        private Map<String, String> map;
    }

    /**
     * HttpClient基本配置
     */
    @Data
    @ToString
    public class HttpClientConfig {
        private String maxConnection;
        private String defaultMaxPerRout;
        private String timeout;
        private String scheme;
        private String host;
        private String port;
    }

    /**
     * {@link HttpClientUtils}工具类初始化
     */
    @Configuration
    @ConditionalOnClass(HttpClientUtils.class)
    public class HttpClientUtilsInit {
        @Autowired
        public HttpClientUtilsInit(UriConfig uriConfig, HttpClientConfig httpClientConfig) {
            HttpClientUtils.init(uriConfig, httpClientConfig);
        }
    }

}
